// pages/forgotPad/forgotPad.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    showModal: false,
    canSend: true,
    sendText: '发送',
    timer: null,
    SurplusTime: 60,
    phone:'',
    phoneLater:'',
    code:''

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('userInfo') != ''){
      this.setData({
        userInfo: JSON.parse(wx.getStorageSync('userInfo'))
      })
    }
    
  },
  changeInput: function (e) {
    if (e.detail.value > 11) {
      e.detail.value = e.detail.value.slice(0, 11)
    }
    var later = e.detail.value.substring(e.detail.value.length - 4, e.detail.value.length);
    this.setData({
      phone: e.detail.value,
      phoneLater: later
    })
  },
  getCodeInfo: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  sendMessage: function (e) {
    if (this.data.SurplusTime == 60) {
      this.setData({ canSend: false, sendText: '60秒后重新发送' });
      var that = this;
      this.timer = setInterval(function () {
        that.setData({ SurplusTime: that.data.SurplusTime - 1, sendText: that.data.SurplusTime - 1 + '秒后重新发送' });
        if (that.data.SurplusTime < 0) {
          clearInterval(that.timer);
          that.setData({ canSend: true, sendText: '发送' });
        }
      }, 1000)
    }
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyPhoneCode/sendPhoneCode',
      mothod: 'POST',
      data: {
        phone: _this.data.phone
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if(res.data.success){
          wx.setStorageSync('sessionId', res.data.sessionId)
        }
        else{
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  verificationCancel: function () {
    clearInterval(this.timer);
    this.setData({ showModal: false, SurplusTime: 60, canSend: true, sendText: '发送'});
  },
  verificationConfirm: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyPhoneCode/checkPhoneCode',
      data: {
        phone: _this.data.phone,
        code: _this.data.code
      },
      mothod: 'POST',
      header: {
        'Cookie': wx.getStorageSync('sessionId'),
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          _this.setData({ showModal: false });
          wx.navigateTo({
            url: '/pages/regSetPad/regSetPad?isSetNewPad=true&navTitle=设置新密码&phone=' + _this.data.phone
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    
  },
  // 校验手机号
  checkMobile: function (sMobile) {
    if (!(/^[1][3,4,5,7,8][0-9]{9}$/.test(sMobile))) {
      return false;
    }
    else {
      return true;
    }
  },
  nextStep: function () {
    var _this = this;
    if (_this.data.phone == '' || !this.checkMobile(_this.data.phone)) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none',
        duration: 2000
      })
    }
    else {
      wx.request({
        url: app.globalData.httpApiCar + 'tGyUserInfo/checkPhoneIsRegister',
        data: {
          phone: _this.data.phone
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          if (res.data.success){
            wx.showToast({
              title: res.data.error,
            })
            setTimeout(function () {
              wx.hideToast();
            }, 2000)
          }
          else{
            _this.setData({ showModal: true });
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})